package com.asnike.selector{
	import com.greensock.TweenMax;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.getDefinitionByName;

	public class GallerySelector extends BasicSelector{
		
		private var _images:Array = [];
		private var _frame:Shape;
		private var _currentIndex:int;
		private var _imageClickCallback:Function;
		
		public function GallerySelector(){
			initEvent();
		}
		private function initEvent():void{
			addEventListener( Event.ADDED_TO_STAGE, addedToStage );
		}
		private function addedToStage( $e:Event ):void{
			initFrame();
			initMask();
		}
		private function initFrame():void{
			_frame = new Shape;
			_frame.graphics.lineStyle( 1, 0 );
			_frame.graphics.drawRect( 0, 0, 764, 316 );
			_frame.graphics.endFill();
			_frame.x = 17;
			_frame.y = 178;
			addChild( _frame );
		}
		private function initMask():void{
			var maskShape:Shape;
			
			maskShape = new Shape;
			maskShape.graphics.beginFill( 0 );
			maskShape.graphics.drawRect( 0, 0, 765, 317 );
			maskShape.graphics.endFill();
			addChild( maskShape );
			mask = maskShape;
			maskShape.x = 17;
			maskShape.y = 178;
		}
		public function setUpperImage( $id:String ):void{
			var cls:Class, upper:Bitmap;
			
			cls = getDefinitionByName( $id ) as Class;
			upper = new Bitmap( new cls );
			addChild( upper );
		}
		
		override public function setData($data:XML):void{
			setImageData( $data[0] );
		}
		
		public function setImageData( $xml:XML ):void{
			loadImages( $xml );
		}
		
		private function loadImages( $data:XML ):void{
			var loader:Loader, totalCount:int, currentCount:int;
			
			totalCount = $data.image.length();
			
			loader = new Loader;
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, function( $e:Event ):void{
				++currentCount;
				
				_images[_images.length] = $e.target.content.bitmapData.clone();
				
				if( currentCount < totalCount ){
					loader.load( new URLRequest( $data.image[currentCount].url.toString() ) );
				}else{
					createImages();
				}
			} );
			loader.load( new URLRequest( $data.image[currentCount].url.toString() ) );
		}
		private function createImages():void{
			var i:int, j:int, imageContainer:Sprite;
			
			
			for( i = 0, j = _images.length ; i < j ; ++i ){
				imageContainer = createImageContainer( _images[i] );
				imageContainer.name = 'image' + i;
//				imageContainer.x = ( i + 1 )*imageContainer.width/4 - imageContainer.width;
				imageContainer.x = 17 +  ( ( i + 1 )*stage.stageWidth/j - imageContainer.width );
				imageContainer.y = 178;
				addChildAt( imageContainer, 0 );
			}
			setChildIndex( _frame, numChildren - 1 );
		}
		private function createImageContainer( $bitmapData:BitmapData ):Sprite{
			var image:Bitmap, imageContainer:Sprite;
			
			image = new Bitmap( $bitmapData );
			imageContainer = new Sprite;
			imageContainer.buttonMode = true;
			imageContainer.addChild( image );
			imageContainer.addEventListener( MouseEvent.MOUSE_OVER, mouseOver );
			imageContainer.addEventListener( MouseEvent.CLICK, mouseClick );
			
			return imageContainer;
		}
		private function mouseOver( $e:MouseEvent ):void{
			_currentIndex = $e.target.name.substr( -1 );
			showImage( _currentIndex, $e.target.width );
		}
		private function showImage( $index:int, $width:Number ):void{
			var i:int, j:int;
			for( i = 0, j = _images.length ; i < j ; ++i ){
				//				setChildIndex( getChildByName( 'image' + i ), i );
				if( i < $index ){
					//					moveImageContainer( getImageContainer( i ), i*50 );
					moveImageContainer( getImageContainer( i ), ( i + 1 )*( stage.stageWidth - $width )/( j - 1 ) - $width );
				}else if( i === $index ){
					//					moveImageContainer( getImageContainer( i ), i*50 );
					moveImageContainer( getImageContainer( i ), i*( stage.stageWidth - $width )/( j - 1 ) );
				}else if( i > $index ){
					//					moveImageContainer( getImageContainer( i ), 764 - ( j - i )*50 );
					//					moveImageContainer( getImageContainer( i ), i*50 );
					moveImageContainer( getImageContainer( i ), i*( stage.stageWidth - $width )/( j - 1 ) );
				}
			}
		}
		private function moveImageContainer( $target:DisplayObject, $x:Number ):void{
			TweenMax.to( $target, .4, {x:$x} );
		}
		private function getImageContainer( $index:int ):DisplayObject{
			return getChildByName( 'image' + $index );
		}
		private function mouseClick( $e:MouseEvent ):void{
			if( _imageClickCallback is Function ){
				_imageClickCallback( int( $e.target.name.substr( -1 ) ) );
			}
		}
		
		public function setImageClickCallback( $function:Function ):void{
			_imageClickCallback = $function;
		}
		
		public function select( $index:int ):void{
			showImage( $index, _images[0].width );
		}
		
		public function show():void{
			visible = true;
		}
		public function hide():void{
			visible = false;
		}
		
		public function getResult():int{
			return _currentIndex;
		}
	}
}